﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Negocios;

public partial class ManterVideo : System.Web.UI.Page
{
    VideoBLL vBLL = new VideoBLL();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btNovo_Click(object sender, EventArgs e)
    {
        ddlEspecificacao.Enabled = true;
        btGravar.Enabled = true;
        btCancelar.Enabled = true;
        tbLocalizacao.Enabled = true;
        ddlGenero.Enabled = true;
        tbNome.Enabled = true;
        ddlAutor.Enabled = true;
        ddlFormato.Enabled = true;
        ddlSoftware.Enabled = true;

    }
    private void cancelar()
    {
        tbCodigo.Text = "";
        ddlEspecificacao.SelectedValue = "0";
        tbNome.Text = "";
        tbLocalizacao.Text = "";
        ddlGenero.SelectedValue = "0";
        ddlFormato.SelectedValue = "0";
        ddlAutor.SelectedValue = "0";
        lbErro.Text = "";

        ddlEspecificacao.Enabled = false;
        ddlAutor.Enabled = false;
        ddlFormato.Enabled = false;
        tbLocalizacao.Enabled = false;
        ddlSoftware.Enabled = false;
        ddlGenero.Enabled = false;
        tbNome.Enabled = false;
        btCancelar.Enabled = false;
        btGravar.Enabled = false;
        btNovo.Enabled = true;
    }
    protected void btGravar_Click(object sender, EventArgs e)
    {
        try
        {
            
            if (tbCodigo.Text == "")
            {     
                Video v = new Video(tbNome.Text, tbLocalizacao.Text, new Genero(Convert.ToInt32(ddlGenero.SelectedValue)), new Autor(Convert.ToInt32(ddlAutor.SelectedValue)), 
                    new Formato(Convert.ToInt32(ddlFormato.SelectedValue)), new Especificacao(Convert.ToInt32(ddlEspecificacao.SelectedValue)), 
                    new SoftwareDeReproducao(Convert.ToInt32(ddlSoftware.SelectedValue)), new Usuario(Session["usuario"].ToString()));
                vBLL.Insere(v);
            }
            else
            {
                Video v = new Video(Convert.ToInt32(tbCodigo.Text), tbNome.Text, tbLocalizacao.Text, new Genero(Convert.ToInt32(ddlGenero.SelectedValue)), 
                    new Autor(Convert.ToInt32(ddlAutor.SelectedValue)), new Formato(Convert.ToInt32(ddlFormato.SelectedValue)),
                    new Especificacao(Convert.ToInt32(ddlEspecificacao.SelectedValue)), new SoftwareDeReproducao(Convert.ToInt32(ddlSoftware.SelectedValue)), new Usuario(Session["usuario"].ToString()));
                vBLL.Atualizar(v);
            }
            cancelar();
            btNovo.Enabled = true;
            GridView1.DataBind();

        }
        catch (Exception ex)
        {
            lbErro.Text = ex.Message;
        }
    }
    protected void btCancelar_Click(object sender, EventArgs e)
    {
        cancelar();
    }
    protected void lbAtualiza_Click(object sender, EventArgs e)
    {
        try
        {
            int id = Convert.ToInt32(((GridViewRow)((DataControlFieldCell)((LinkButton)sender).Parent).Parent).Cells[0].Text);
            Video v = vBLL.Consultar(id);
            tbCodigo.Text = v.Codigo.ToString();
            tbNome.Text = v.Nome;
            tbLocalizacao.Text = v.Localizacao;
            ddlGenero.SelectedValue = v.Genero.Codigo.ToString();
            ddlAutor.SelectedValue = v.Autor.Codigo.ToString();
            ddlFormato.SelectedValue = v.Formato.Codigo.ToString();
            ddlEspecificacao.SelectedValue = v.Especificacao.Codigo.ToString();
            ddlSoftware.SelectedValue = v.Software.Codigo.ToString();
            GridView1.DataBind();

            tbNome.Enabled = true;
            tbLocalizacao.Enabled = true;
            ddlGenero.Enabled = true;
            ddlAutor.Enabled = true;
            ddlFormato.Enabled = true;
            ddlEspecificacao.Enabled = true;
            ddlSoftware.Enabled = true;
            btGravar.Enabled = true;
            btCancelar.Enabled = true;
            btNovo.Enabled = false;
        }
        catch (Exception ex)
        {
            lbErro.Text = ex.Message;
        }
    }
    protected void lbRemove_Click(object sender, EventArgs e)
    {
        try
        {
            int id = Convert.ToInt32(((GridViewRow)((DataControlFieldCell)((LinkButton)sender).Parent).Parent).Cells[0].Text);
            Video v = new Video(id);
            vBLL.Excluir(v);
            GridView1.DataBind();
        }
        catch (Exception ex)
        {
            lbErro.Text = ex.Message;
        }
    }
}
